# -*- coding: utf-8 -*-
class BankController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:newBank,:editBank,:bankLock]

  #物流设置页面
  def bankList

  end

  #显示添加物流页面
  def newBank

  end

  #显示编辑物流页面
  def editBank

  end

  def getAllBank
    start = params[:start]
    limit = params[:limit]
    if params[:query].nil?
      queryStr = ""
    else
      queryStr = params[:query]
    end
    banks = Bank.name_like queryStr
    if start.nil?
      banksNow = banks
    else
      banksNow = banks.paginate start,limit
    end

    jsonStr="{totalProperty:"+banks.length.to_s+",root:"+banksNow.to_json(:include=>[:editor])+"}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #添加物流页面
  def newBank

  end

  def create
    begin
      bank = Bank.new()
      bank.company_id = session[:company_id]
      bank.department_id = session[:department_id]
      bank.employee_id = session[:employee_id]
      bank.name = params[:name]
      bank.phone = params[:phone]
      bank.address = params[:address]
      bank.description = params[:description]
      bank.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  #编辑前的数据准备
  def getBankById
    bank = Bank.find(params[:id])
    rootStr = bank.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #把编辑后的数据，更新到数据库中
  def update
    begin
      bank = Bank.find(params[:id])
      bank.name = params[:name]
      bank.phone = params[:phone]
      bank.address = params[:address]
      bank.description = params[:description]
      bank.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁销售类型
  def bankLock
    bank = Bank.find(params[:id])
    if bank.is_locked == 1
      bank.is_locked = 0
    else
      bank.is_locked = 1
    end
    bank.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
end
